package com.qzdsoft.erpcloud.controller.pu;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.com.CustomerSupplierContacts;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.pu.PurchaseInquiryService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.TablePermission;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryEditInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryQueryInfo;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;

/**
 * 
 * 采购询价单controller
 *
 * <p>detailed comment
 * @author  2017年9月27日
 * @see
 * @since 1.0
 */
@Controller
@RequestMapping("/pu/purchaseinquiry")
public class PurchaseInquiryController extends BaseController{
	
    private static final Logger logger = LoggerFactory.getLogger(PurchaseInquiryController.class);
	private static final String BASEPATH = "pu/purchaseinquiry";

	@Autowired
	private PurchaseInquiryService purchaseInquiryService;
	
	@Autowired
	private CustomerSupplierService customerSupplierService;
	
	@Autowired
	private CustomerSupplierStatementService customerSupplierStatementService;
	
	/**
     * 采购询价单首页
     * @return
     */
    @TablePermission(id="3")
    @Permission(validate=false)
	@RequestMapping("/index")
	public ModelAndView index() {
        LoginUser user = getUser();
		ModelAndView mv = new ModelAndView();
		List<TypeInfo> status = PurchaseOrderStatus.ALL;
		mv.addObject("status",status);
		mv.addObject("companys", user.getCompanys());
		mv.setViewName(BASEPATH+"/index");
		return mv;
	}
	
    /**
     * 采购询价单表格列表查询
     * @param info 查询条件封装
     * @return
     */
    //@SysLog(description="采购询价单表格列表查询")
	@ResponseBody
	@RequestMapping("/list")
	public LayTableResponse<PurchaseInquiryInfo> list(PurchaseInquiryQueryInfo info){
	    logger.debug("查询采购询价单，参数：{}",info);
        //如果初始化查询返回无数据空页
        if(info.isFirst()) {
            return LayTableResponse.nullDataPage();
        }else {
            return purchaseInquiryService.findPurchaseInquirysBy(info);
        }
	}
	/**
     * 采购询价单编辑
     * @param info
     * @return
     */
    @SysLog(description="采购询价单编辑")
	@ResponseBody
	@RequestMapping("/edit")
	public Response<String> edit(PurchaseInquiryEditInfo info){
		return purchaseInquiryService.edit(info);
	}
	
    /**
     * 采购询价单新增页面
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/addinit")
    public ModelAndView addinit(Integer companyid) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("companyid",companyid);
        //mv.addObject("types",PurchaseOrderSourceType.ALL);
        mv.setViewName(BASEPATH + "/add");
        return mv;
    }
    
    /**
     * 供应商结算方式查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/querySettleMethod/{supplierid}")
    public List<CustomerSupplierStatementInfo> querySettleMethod(@PathVariable("supplierid")Integer supplierid) {
        //供应商结算方式
       LoginUser user = getUser();
       Integer companyid = user.getCompanyId();
       List<CustomerSupplierStatementInfo> customerSupplierStatements = customerSupplierStatementService.selectBy(companyid, supplierid);
       return customerSupplierStatements;
    }
    
    /**
     * 供应商税率信息查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/queryTax/{supplierid}/{companyid}")
    public TypeInfo queryTax(@PathVariable("supplierid")Integer supplierid,@PathVariable("companyid")Integer companyid) {
        return customerSupplierService.selectTaxInfo(companyid, supplierid);
        
    }
    /**
     * 采购询价单新增提交
     * @param 采购询价单新增信息
     * @return
     */
    @SysLog(description="新增采购询价单")
    @ResponseBody
    @RequestMapping("/add")
    public Response<String> add(PurchaseInquiryEditInfo info){
        logger.debug("新增采购询价单:{}",info);
        try {
            info.setCreatorId(getUser().getId());
            return  purchaseInquiryService.add(info);
        }catch(Exception ex) {
            logger.error("新增采购询价单错误，异常信息：{}",ExceptionUtil.getStackTrace(ex));
        }
        return new Response<>(ResultEnum.ERROR);
    }
  
    /**
     * 修改采购询价单页面跳转
     * @param companyid 公司id
     * @param id 采购询价单id
     * @return
     */
    @Permission(validate=false)
	@RequestMapping("/{companyid}/{id}")
	public ModelAndView detail(@PathVariable("companyid")String companyid,@PathVariable("id")Integer id) 
	{
        logger.debug("查询采购询价单详情：companyid={},id={}",companyid,id);
        ModelAndView mv = new ModelAndView();
        PurchaseInquiryInfo order = purchaseInquiryService.findPurchaseInquiryBy(id);
        mv.addObject("order", order);
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status", status);
        List<CustomerSupplierContacts> contacts = customerSupplierService.selectAll(order.getSupplierid()+"");
        mv.addObject("contacts",contacts);
        mv.setViewName(BASEPATH + "/edit");
        return mv;
    }
	
	/**
     * 审核采购询价单
     * @param id 采购询价单id
     * @return
     */
    @SysLog(description="审核采购询价单")
    @ResponseBody
    @RequestMapping("/review")
    public Response<PurchaseInquiryInfo> review(HttpServletRequest request,Integer id){
        Integer uid = getUid();
        return purchaseInquiryService.reivew(uid,id);
    }
    
    /**
     * 弃审
     * @param id
     * @return
     */
    @SysLog(description="弃审采购询价单")
    @ResponseBody
    @RequestMapping("/cancelreview")
    public Response<PurchaseInquiryInfo> cancelReview(HttpServletRequest request,Integer id){
        Integer uid = getUid();
        return purchaseInquiryService.cancelReview(uid,id);
    }
    
//    /**
//     * 关闭采购询价单
//     * @param id
//     * @return
//     */
//    @SysLog(description="关闭采购询价单")
//    @ResponseBody
//    @RequestMapping("/close")
//    public Response<String> close(Integer id){
//        return purchaseInquiryService.close(id);
//    }
    
    /**
     * 删除采购询价单
     * @param id
     * @return
     */
    @SysLog(description="删除采购询价单")
    @ResponseBody
    @RequestMapping("/remove")
    public Response<String> remove(Integer id){
        return purchaseInquiryService.remove(id);
    }
}
